package com.yeyv.net.Web.Service.Implement;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.yeyv.net.UsefulEntity.Constant;
import com.yeyv.net.Web.Service.GameAutoAuthService;
import com.yeyv.net.Web.Service.GameCdkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class GameAutoAuthServiceImpl implements GameAutoAuthService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private GameCdkService cdkService;

    @Override
    @DS("game_1")
    public String getLTRole(String user) {
        String result = null;
        try {
            var test = jdbcTemplate.queryForMap("SELECT * FROM actors WHERE accountname like '" + user + "' or actorname like '" + user + "' limit 1");
            result = test.get("actorid").toString();
        } catch (Exception ex) {
            return null;
        }
        return result;
    }

    @Override
    @DS("game_1_gm")
    public String autoAuthLT(String user, String roleid, String cdk) {
        try {
            var result = jdbcTemplate.queryForList("SELECT * FROM `user` WHERE `user` = '" + user + "' or `name` = '" + user + "' or roleid = '" + roleid + "' ");
            if (!result.isEmpty()) {
                return "账号已经授权过了，如果忘记密码请联系 QQ 3379655072 重置密码";
            }
            String password = Constant.random(8);
            var twoAuth = cdkService.useCdk("SGLT", cdk);
            if (!twoAuth) {
                return "授权错误：CDK ERROR";
            }
            var sql = String.format("INSERT INTO user(user,password,name,status,qu,roleid,addname,endtime) values ('%s','%s','%s','vip5', %d , '%s' , 'auto' , '2033-04-15')", user, password, user, 1, roleid);
            jdbcTemplate.execute(sql);
            return "账号授权成功，密码为：" + password;
        } catch (Exception ex) {
            ex.printStackTrace();
            return "授权发生异常，请联系 QQ 3379655072 ! 错误信息：" + ex.getMessage();
        }
    }

    @DS("game_sszg_1")
    public String autoAuthSSZG1(String user, String cdk) {
        try {
            var result = jdbcTemplate.queryForMap("SELECT * FROM `role` WHERE `account` = '" + user + "'");
            var twoAuth = cdkService.useCdk("SSZG", cdk);
            if (!twoAuth) {
                return "授权错误：CDK ERROR";
            }
            jdbcTemplate.execute("UPDATE `role` SET `vip` = 1 WHERE `account` = '" + user + "'");
            return "授权成功！";
        } catch (Exception e) {
            return "未找到 账号/角色 或发生异常 => 请联系QQ 3379655072" + e.getMessage();
        }
    }

    @DS("game_sszg_2")
    public String autoAuthSSZG2(String user, String cdk) {
        try {
            var result = jdbcTemplate.queryForMap("SELECT * FROM `role` WHERE `account` = '" + user + "'");
            var twoAuth = cdkService.useCdk("SSZG", cdk);
            if (!twoAuth) {
                return "授权错误：CDK ERROR";
            }
            jdbcTemplate.execute("UPDATE `role` SET `vip` = 1 WHERE `account` = '" + user + "'");
            return "授权成功！";
        } catch (Exception e) {
            return "未找到 账号/角色 或发生异常 => 请联系QQ 3379655072" + e.getMessage();
        }
    }
}
